﻿using System;
using Acme.Common.Services;
using Acme.MVVM.Scenarios.Infrastructure;
using Acme.MVVM.Scenarios.Views;
using Acme.Prism.Infrastructure;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;

namespace Acme.MVVM.Scenarios
{
    public class ScenarioModule : IModule
    {

        readonly IUnityContainer _container;
        readonly IRegionManager _regionManager;

        public ScenarioModule(IUnityContainer container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
        }

        void IModule.Initialize()
        {
            //load container
            _container.RegisterType<IDialogService, ModalDialogService>(new TransientLifetimeManager());
            _container.RegisterType(typeof(IEventAggregatorResolver<>), typeof(EventAggregatorResolver<>));

            //load MainRegion using Prism View Discovery
            RegisterView(() => _container.Resolve<DialogServiceView>());
            RegisterView(() => _container.Resolve<DialogInteractionRequestView>());
            RegisterView(() => _container.Resolve<EventAggregatorView>());
            RegisterView(() => _container.Resolve<ListBoxItemExecuteCommandView>());

            _regionManager.RegisterViewWithRegion(Constants.PubContentRegionName, () => _container.Resolve<PublisherView>());
            _regionManager.RegisterViewWithRegion(Constants.SubContentRegionName, () => _container.Resolve<SubscriberView>());
        }

        void RegisterView(Func<Object> resolveDelegate)
        {
            _regionManager.RegisterViewWithRegion(Constants.MainContentRegionName, resolveDelegate);
        }
    }
}
